package com.lhj.library.application.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.fastjson.JSON;
import com.lhj.library.application.convert.RecordDtoConverter;
import com.lhj.library.application.dto.RecordDTO;
import com.lhj.library.common.entity.PageResult;
import com.lhj.library.common.entity.Result;
import com.lhj.library.domain.entity.RecordBO;
import com.lhj.library.domain.service.RecordDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/record")
@Slf4j
public class RecordController {

    @Resource
    private RecordDomainService recordDomainService;

    /**
     * 查询列表（借出/归还）
     *
     * @param recordDTO
     * @return
     */
    @PostMapping("/selectByPage")
    public Result<PageResult<RecordDTO>> selectByPage(@RequestBody RecordDTO recordDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RecordController.selectByPage.dto:{}", JSON.toJSONString(recordDTO));
            }
            RecordBO recordBO = RecordDtoConverter.INSTANCE.convertDtoToRecordBo(recordDTO);
            PageResult<RecordBO> boPageResult = recordDomainService.selectByPage(recordBO);
            PageResult<RecordDTO> recordDTOPageResult = RecordDtoConverter.INSTANCE.convertBoToDtoPage(boPageResult);
            return Result.ok(recordDTOPageResult);
        } catch (Exception e) {
            return Result.fail("查询失败");
        }
    }

}
